package com.shamine.kuaiqiao.blink.receiver

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.support.v4.app.NotificationCompat
import com.shamine.kuaiqiao.blink.R
import com.shamine.kuaiqiao.blink.gallery.GalleryActivity

class BlinkReceiver: BroadcastReceiver() {

    companion object {
        val APP_NOTIFICATION_ID = "com.shamine.kuaiqiao.reminder"
        val APP_CHANNEL_NAME = "blinkChannel"
        val ACTION = "com.shamine.kuaiqiao.action_reminder"
    }
    override fun onReceive(context: Context?, intent: Intent?) {
        if (intent!!.action == ACTION){
            val pendingIntent = PendingIntent.getActivity(
                    context!!,
                    1,
                    Intent(context, GalleryActivity::class.java),
                    PendingIntent.FLAG_UPDATE_CURRENT)
            val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
                val builder = NotificationCompat.Builder(context,APP_NOTIFICATION_ID)
                        .setAutoCancel(true)
                        .setContentIntent(pendingIntent)
                        .setSmallIcon(R.drawable.ic_camera_white)
                        .setContentTitle(context.getString(R.string.app_name))
                        .setContentText(context.getString(R.string.notification_content))
                notificationManager.notify(1, builder.build())
            }else{
                val notificationChannel = NotificationChannel(APP_NOTIFICATION_ID,
                        APP_CHANNEL_NAME,
                        NotificationManager.IMPORTANCE_HIGH)
                notificationChannel.enableVibration(true)
                notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
                notificationManager.createNotificationChannel(notificationChannel)
                val builder = NotificationCompat.Builder(context,APP_NOTIFICATION_ID)
                        .setAutoCancel(true)
                        .setContentIntent(pendingIntent)
                        .setSmallIcon(R.drawable.ic_camera_white)
                        .setContentTitle(context.getString(R.string.app_name))
                        .setContentText(context.getString(R.string.notification_content))
                notificationManager.notify(1, builder.build())
            }
        }
    }
}